/**
 * Created by gzf on 2015/10/9.
 */
user_config = {
    currPageNo:1,
    canNext:true,
    status:{0:'未支付',1:'已发布',2:'已取消',3:'进行中',4:'已完成'},
    servTypes:{'pb':'顺购','qe':'排队','bw':'顺捎','dh':'顺驾','hk':'家政','rp':'维修','st':'接送','hw':'其他'}
}
function addOrderList(type,pageNo){
    isLoading(true);
    switch(type){
        case 'server':
            $.getJSON('server.json?pageNo='+pageNo,function(data){
                dealOrderData(data);
            });
            break;
        case 'pub':
            $.getJSON('pub.json?pageNo='+pageNo,function(data){
                dealOrderData(data);
            });
            break;
    }
}
function dealOrderData(orderJson){
    if(orderJson.status){
        var html = '';
        for(var i= 0,orderList=orderJson.data,len=orderList.length;i<len;i++){
            var orderInfo = getOrderInfo(orderList[i]);
            if(orderInfo.id){
                html += '<a class="container dc list-box" href="XXX/' + orderInfo.id + '">'
                    +       '<div class="title">' + orderInfo.theme + '</div>'
                    +       '<div>'
                    +           '<div class="col-xs-4 desc">服务时间:</div>'
                    +           '<div class="col-xs-8 col-xs-pull-1">' + orderInfo.serverTime + '</div>'
                    +       '</div>'
                    +       '<div>'
                    +           '<div class="col-xs-4 desc">跑腿费用:</div>'
                    +           '<div class="col-xs-4 col-xs-pull-1 fee">' + orderInfo.price + '元</div>'
                    +           '<div class="col-xs-4 text-right order-status">' + orderInfo.statusTxt + '</div>'
                    +       '</div>'
                    +       '<div class="serv-icon ' + orderInfo.rawtype + '">' + orderInfo.rawtypeTxt + '<b></b></div>'
                    +   '</a>';
            }
        }
        setTimeout(function(){
            $('.my-order-list .list-content').append($(html));
            isLoading(false);
        },500);
    }else{
        setTimeout(function(){
            showMessage(orderJson.msg);
            isLoading(false);
            user_config.currPageNo--;
        },500);
    }
}
function isLoading(flag){
    if(flag){
        user_config.canNext = false;
        user_config.currPageNo == 1 || $('.load-add').addClass('load-adding').text('正在加载...');
    }else{
        user_config.canNext = true;
        user_config.currPageNo == 1 || $('.load-add').removeClass('load-adding').text('加载更多');
    }
}
function getOrderInfo(info){
    var orderInfo = {};
    orderInfo.id = info.id;
    orderInfo.rawtype = info.rawtype;
    orderInfo.rawtypeTxt = user_config.servTypes[orderInfo.rawtype];
    orderInfo.price = info.serve_price;
    orderInfo.statusTxt = user_config.status[info.status];
    orderInfo.theme = info.detail.theme;
    //不同类型服务，服务时间字段不同
    var time = orderInfo.rawtype == 'bw'? info.detail.get_goods_time :
               orderInfo.rawtype == 'pb'? info.detail.goods_latest_time :
               orderInfo.rawtype == 'dh' ? info.detail.drive_time :
               orderInfo.rawtype == 'qe' ? info.detail.queue_time :
               orderInfo.rawtype == 'rp' ? info.detail.rp_time :
               orderInfo.rawtype == 'st' ? info.detail.st_time :
               orderInfo.rawtype == 'hk' ? info.detail.hk_time :
               orderInfo.rawtype == 'hw' ? info.detail.hw_starttime :
                                                '0000-00-00 00:00:00';
    orderInfo.serverTime = time.substring(time.indexOf('-')+1,time.lastIndexOf(':'));
    return orderInfo;
}
function showMessage(message){
    var content = {};
    content.title = '';
    content.text = message;
    content.animation = 'slide-from-bottom';
    content.timer = 1000;
    content.showConfirmButton = false;
    swal(content);
}
var addMore = $('.load-add').hammer();
addMore.on('tap',function(){
    if(user_config.canNext){
        addOrderList($('.load-add').attr('data-type'),++user_config.currPageNo);
    }
});

